package com.yonyou.pmclouds.alterandvisa.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.alterandvisa.entity.AlterAndVisaListViewVO;
import com.yonyou.pmclouds.alterandvisa.entity.AlterAndVisaVO;
import com.yonyou.pmclouds.alterandvisa.rmiitf.AlterAndVisaMaintain;
import com.yonyou.pmclouds.alterandvisa.rmiitf.AlterAndVisaQuery;
import com.yonyou.pmclouds.alterandvisacategory.entity.AlterAndVisaCategoryVO;
import com.yonyou.pmclouds.alterandvisacategory.rmiitf.AlterAndVisaCategoryQuery;
import com.yonyou.pmclouds.basecom.annotation.Insert;
import com.yonyou.pmclouds.basecom.entity.AdditionalPropertiesVOWrapper;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.service.CommonPropertiesProcessor;
import com.yonyou.pmclouds.offline.aspect.Offline;
import com.yonyou.pmclouds.offline.entity.OffLineDataKeyConst;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.team.entity.TeamMemberVO;
import com.yonyou.pmclouds.team.rmiitf.TeamEnterpriseQuery;
import com.yonyou.pmclouds.team.rmiitf.TeamMemberQuery;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 签证申请
 */
@RequestMapping("/mobile/alterandvisa")
@RestController
@Offline(dataFrom = OffLineDataKeyConst.DATA_ALTER_AND_VISA_CATEGORY)
public class AlterAndVisaController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private AlterAndVisaMaintain maintain;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private AlterAndVisaQuery alterAndVisaQuery;

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private TeamMemberQuery teamMemberQuery;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private TeamEnterpriseQuery enterpriseQuery;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private AlterAndVisaCategoryQuery categoryQuery;

    @Autowired
    private UserInfoQuery userQuery;
    @Autowired
    private EnterpriseInfoQuery enterpriseInfoQuery;
    @Autowired
    private CommonPropertiesProcessor propertiesProcessor;


    private static final String[] USER_ID_FIELDS = {AlterAndVisaVO.CREATOR};
    private static final String[] USER_NAME_FIELDS = {AlterAndVisaVO.CREATOR_NAME};

    /**
     * 提交
     */
    @Insert
    @RequestMapping(value = "/add")
    @PMApprove
    public AlterAndVisaVO add(@RequestBody AlterAndVisaVO alterAndVisaVO) throws BusinessException {
        if(alterAndVisaVO == null) return null;
        return maintain.insert(alterAndVisaVO);
    }

    /**
     * 驳回修改
     */
    @RequestMapping(value = "/update")
    @PMApprove(trigger = "commit")
    public AlterAndVisaVO update(@RequestBody AlterAndVisaVO alterAndVisaVO) throws BusinessException {
        if(alterAndVisaVO == null) return null;
        return maintain.update(alterAndVisaVO);
    }

    /**
     * 获取变更签证列表
     */
    @RequestMapping(value = "/getList")
    @ResponseBody
    public AdditionalPropertiesVOWrapper[] getList(@RequestParam("pkProject") String pkProject) throws BusinessException {
        if(StringUtils.isEmpty(pkProject)){
            return new AdditionalPropertiesVOWrapper[0];
        }
        AlterAndVisaListViewVO[] result = alterAndVisaQuery.getAlterAndVisaListInfo(pkProject);
        userQuery.tranUserInfoField(result, USER_ID_FIELDS, USER_NAME_FIELDS);
        result = result == null ? new AlterAndVisaListViewVO[0] : result;
        return propertiesProcessor.wrapAdditionalProps(result);
    }

    /**
     * 获取变更签证详情
     */
    @RequestMapping(value = "/getDetail")
    @ResponseBody
    public AlterAndVisaVO getDetail(@RequestParam("pkAlterandvisa") String pkAlterandvisa) throws BusinessException {
        if(StringUtils.isEmpty(pkAlterandvisa)){
            return null;
        }
        AlterAndVisaVO result = alterAndVisaQuery.getById(pkAlterandvisa);
        userQuery.tranUserInfoField(new AlterAndVisaVO[]{result}, USER_ID_FIELDS, USER_NAME_FIELDS);
        //企业信息添加
        enterpriseInfoQuery.tranEntInfoField(new AlterAndVisaVO[]{result}, new String[]{AlterAndVisaVO.PK_ENTERPRISE}, new String[]{TeamMemberVO.ENTERPRISE_NAME});
        return result;
    }

    /**
     * 获取签证分类列表
     */
    @RequestMapping(value = "/getCategoryList")
    public AlterAndVisaCategoryVO[] getCategoryList(@RequestParam(name = "pkTenant") String pkTenant, @RequestParam(name = "isVisa") Byte isVisa) throws BusinessException {
        AlterAndVisaCategoryVO[] result = categoryQuery.queryCategoryListInfo(pkTenant, isVisa);
        userQuery.tranUserInfoField(result, USER_ID_FIELDS, USER_NAME_FIELDS);
        result = result == null ? new AlterAndVisaCategoryVO[0] : result;
        return result;
    }

    @Offline()
    public AlterAndVisaCategoryVO[] setTranUserInfo(Object object){
        AlterAndVisaCategoryVO[] result = (AlterAndVisaCategoryVO[])object;
        userQuery.tranUserInfoField(result, USER_ID_FIELDS, USER_NAME_FIELDS);
        result = result == null ? new AlterAndVisaCategoryVO[0] : result;
        return result;
    }

}
